package com.poizon.utils;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName ThreadPoolSingle
 * @Author NoCheng
 * @Date 2022/1/2 11:17
 * @Description:TODO 线程池(IoDH单例)
 */

public class ThreadPoolSingle {
    //原生的线程池对象
    public final ThreadPoolExecutor poolExecutor;

    //私有化构造器
    private ThreadPoolSingle() {
        poolExecutor = new ThreadPoolExecutor(4, 10,
                3, TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(20),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy());
    }

    //静态内部类
    static class ThreadPoolSingleFactory {
        public static ThreadPoolSingle single = new ThreadPoolSingle();
    }

    //外部获取线程池对象
    public static ThreadPoolSingle getInstance() {
        return ThreadPoolSingleFactory.single;
    }

    //新增任务
    public void addTask(Runnable runnable) {
        poolExecutor.submit(runnable);
    }
}
